<?php
/**
 * Created by PhpStorm.
 * User: l-ht
 * Date: 2018/10/28
 * Time: 14:29
 */

namespace app\index\controller;


use app\index\model\Album;
use app\index\model\User;
use think\Request;
use app\index\model\Thread as ThreadModel;

class Thread extends Base
{
    protected $_thread;

    public function __construct(ThreadModel $thread)
    {
        $this->_thread = $thread;
    }

    //发表帖子
    public function publish(Request $request)
    {
        $data = $request->param();

        $user = User::where('id',$data['uid'])->find();
        if (!$user) {
            return $this->_toError(['msg' => '发布失败']);
        }

        $this->_thread->typeid   = $data['typeid'];
        $this->_thread->region   = $data['city'];
        $this->_thread->subject  = $data['content'];
        $this->_thread->authorid = $data['uid'];
        $this->_thread->region   = $data['city'];
        $this->_thread->district = $data['district'];
        $this->_thread->lat      = $data['lat'];
        $this->_thread->lng      = $data['lng'];

        $this->_thread->save();

        //积分操作
        $this->makePoint($data['uid'],$this->_thread->tid,1);

        return $this->_toSuccess([ 'tid' => $this->_thread->tid ]);
    }
    
     //删除帖子
    public function delThread()
    {
        $uid = input('uid');
        $tid = input('tid');

        $res = $this->_thread->destroy($tid);;
        if($res){
		 //积分操作
         $this->makePoint($uid,$tid,10);	
		 return $this->_toSuccess(['msg' => '成功']);	
		}
       
    }   
    //上传帖子图片
    public function uploadFile(Request $request)
    {

        $tid = $request->only('tid');
        $file = $request->file('uploadfile_ant');


        $info = $file->move('album');

        if ($info) {
            $filename = $info->getFilename();//文件名称

            $album = new Album();
            $album->tid         = $tid['tid'];
            $album->dir         = date("Ymd");
            $album->filename    = $filename;
            $album->save();
        }
    }
    //上传头像
    public function uploadimage(Request $request)
    {

        $uid = input('uid');
		$url =  input('url');
        $file = $request->file('uploadfile_ant');


        $info = $file->move('album');

        if ($info) {
          $filename = $info->getFilename();//文件名称
             $avatar =  $url.'album/'.date("Ymd")."/".$filename;
			 $user = new User;
             $res = $user->where('id',$uid)->update(['avatar' => $avatar]);
			 echo $res;
        }
    }	
}